package cn.jsj.dispose;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jsj.object.FrontCaseInfo;
import cn.jsj.pojo.TblCaseInfo;
import cn.jsj.utils.ProduceJsonData;
import cn.jsj.utils.SysLogger;
 public class SearchCaseInfo extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//调试信息
		cn.jsj.utils.SysLogger.PrintInfo("查询案件信息");

		//获得参数
		String contractName=request.getParameter("contractName").trim();
		contractName=new String(contractName.getBytes("ISO-8859-1"),"UTF-8");
		String signTimeS=request.getParameter("signTimeS").trim();
		signTimeS=new String(signTimeS.getBytes("ISO-8859-1"),"UTF-8");
		String signTimeE=request.getParameter("signTimeE").trim();
		signTimeE=new String(signTimeE.getBytes("ISO-8859-1"),"UTF-8");
		String undertakeLawyer=request.getParameter("undertakeLawyer").trim();
		undertakeLawyer=new String(undertakeLawyer.getBytes("ISO-8859-1"),"UTF-8");
		//调试信息
		cn.jsj.utils.SysLogger.PrintInfo("获得的参数为---------->"+contractName+signTimeS+signTimeE+undertakeLawyer);

		//获得参数：1.第几条开始。2.一页显示几条
		int startNumb=Integer.parseInt(request.getParameter("start"));
		int totalNumb=Integer.parseInt(request.getParameter("limit"));

		//调试信息
		cn.jsj.utils.SysLogger.PrintInfo("start---->"+startNumb+"   total--------->"+totalNumb);

		//获得session对象
		javax.servlet.http.HttpSession session=request.getSession();

		//暂存案件信息条数
		int caseNumber=0;
		//新建案件信息操作对象
		cn.jsj.dao.TblCaseInfoDAO tcd=new cn.jsj.dao.TblCaseInfoDAO();
		//判断用户类型
		int popedomLevel=(Integer)session.getAttribute("popedomLevel");
		String roleName=(String)session.getAttribute("roleName");
		String userName=(String)session.getAttribute("userName");
		int userId=(Integer)session.getAttribute("userId");


		//如果为普通律师
		if(popedomLevel==3&&roleName.equalsIgnoreCase("律师")){
			caseNumber=tcd.getTheCaseInfoNumber(contractName, cn.jsj.utils.TimeDispose.transferDate(signTimeS), cn.jsj.utils.TimeDispose.transferDate(signTimeE), undertakeLawyer, userId);

		}else{
				caseNumber=tcd.getTheCaseInfoNumber(contractName, cn.jsj.utils.TimeDispose.transferDate(signTimeS), cn.jsj.utils.TimeDispose.transferDate(signTimeE), undertakeLawyer,0);
			}

		//暂存案件信息队列
		java.util.List<cn.jsj.pojo.TblCaseInfo> caseInfoList=new java.util.LinkedList<TblCaseInfo>();

		//如果为普通律师
		if(popedomLevel==3&&roleName.equalsIgnoreCase("律师")){
			caseInfoList=tcd.getTheCaseInfo(contractName, cn.jsj.utils.TimeDispose.transferDate(signTimeS), cn.jsj.utils.TimeDispose.transferDate(signTimeE), undertakeLawyer, startNumb, totalNumb,userId);

		}else{
			caseInfoList=tcd.getTheCaseInfo(contractName, cn.jsj.utils.TimeDispose.transferDate(signTimeS), cn.jsj.utils.TimeDispose.transferDate(signTimeE), undertakeLawyer, startNumb, totalNumb,0);
		}

		//前台需要的案件信息队列
		java.util.List<cn.jsj.object.FrontCaseInfo> frontCaseList=new java.util.LinkedList<FrontCaseInfo>();

		//将从数据库里面取出来的案件信息对象转化为前台需要的对象
		cn.jsj.object.rebuild.CaseInfoRebuild cir=new cn.jsj.object.rebuild.CaseInfoRebuild();
		cn.jsj.object.FrontCaseInfo fci=new cn.jsj.object.FrontCaseInfo();
		for(int i=0;i<caseInfoList.size();i++){
			fci=cir.rebuildCaseInfo(caseInfoList.get(i));
			frontCaseList.add(fci);
		}

		//改造成JSON型数据
		ProduceJsonData pjd=new ProduceJsonData();
		String resultData=	pjd.jsonFromList(frontCaseList,caseNumber);

		//调试信息
		SysLogger.PrintInfo(resultData);
		//输出到客户端
		java.io.PrintWriter pw=response.getWriter();
		pw.println(resultData);
		pw.flush();
		pw.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
}